package inv.cal.view;

import inv.cal.logic.Day;
import inv.cal.logic.EventsInfo;
import inv.cal.logic.Manager;

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JTable;

public class DayCellRenderer extends NumberCellRenderer {
	private static final long serialVersionUID = -2205215495794387657L;
	private static final SimpleDateFormat sdf	= new SimpleDateFormat("dd");
	Manager manager;
	
	public DayCellRenderer(Manager manager) {
		super();
		this.manager	= manager;
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		if(value == null && isSelected)
			isSelected = false;
		
		Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
				row, column);
		
		if(value instanceof Day){
			Day day		= (Day)value;
			DayInfoRenderer renderer	= new DayInfoRenderer();
			List<EventsInfo> info		= manager.getDays(day.getDate());
			c	= renderer.render(info, c, table, value, isSelected, hasFocus, row, column);
			
			if(manager.isHoliday(day.getDate())){
				ViewUtil.highlightHoliday((JLabel)c);
			}
			
			Date date	= manager.getSelectedDate();
			if(value != null && date != null){
				String day1	= sdf.format(((Day)value).getDate());
				String day2 = sdf.format(date);
				if(day1.equals(day2)){
					ViewUtil.highlightSelected((JLabel)c);
				}
			}
			
			if(isSelected)
				manager.setReturnDate(day.getDate());
		}
		
		return c;
	}
	
}
